
#include "header.h"


int main()
{
	CDataFile	*pDataFile;

	pDataFile = NULL;

	printf("Test Program for working with the new \nDataObject file loading library.\n\n");
	
	pDataFile = new CDataFile();
	if(pDataFile == NULL)
	{
		printf("Error allocating space to the CDataFile Class.\n");
		return 0;
	}

	// Load the file and make sure to check for an error...
	if(pDataFile->LoadDataFile("data.txt", 0))
	{
		printf("There was a problem loading your data file.\n");
		return 0;
	}

	// Print out the loaded data
	pDataFile->PrintCollection();

	// Print Age
	printf("\n\nAge: %s\n", pDataFile->GetString("Interests", "Games"));

	// unload the data object now
	pDataFile->UnloadDataFile();

	while(1);

	return 0;
}

// ----------------------------------------------
// RemoveSpaces()
// 
// Takes a source char string and removes all the
// spaces from it. This method DOES change the 
// source string.
// ----------------------------------------------
int RemoveSpaces(char *src, int flags)
{
	bool	bInQuotes = false;	// flag set when current pos is within quotes.
	char	*dest;				// Pointer to destination string
	int		len;				// Length of source string
	int		destPos = 0;
	int curPos = 0;

	len = strlen(src);
	dest = (char *)malloc(len);
	if(!dest)
	{
		// Error allocating memory for dest string
		return EXIT_FAILURE;
	}

	for(int i = 0; i < len; i++)
	{
		// check if we are within quotes
		if(bInQuotes)
		{
			// copy everything until closing quote
			dest[destPos] = src[i];
			destPos++;
			if(src[i] == '"')
				bInQuotes = false;

		} else{
			if(src[i] != ' ')
	//		if(isspace(src[i]))
			{
				dest[destPos] = src[i];
				destPos++;
				if(src[i] == '"')
					bInQuotes = true;
			}
		}
	} // for(int i = 0; i < len; i++)
	dest[destPos] = '\0';

	// Need to remove any newline from the string
	while(dest[curPos])
	{
		if(dest[curPos] == '\n')
		{
			dest[curPos] = '\0';
			break;
		}
		curPos++;
	}

	//memcpy(src, '\0', len);
	strcpy(src, dest);

	//free(dest);
	return 0;
}